package com.hkd.common.api;

import lombok.Data;

import java.io.Serializable;
import java.util.Map;

/**
 * 统一返回类型
 *
 * @author HKD
 * @date 2022/8/20 17:50
 */
@Data
public class ApiResponse<T> implements Serializable {
    private static final int SUCCESS_CODE=0;
    private static final int ERROR_CODE=1;

    private String message;
    private int code;
    private T data;

    public static ApiResponse ok(){
        ApiResponse resp = new ApiResponse();
        resp.code=SUCCESS_CODE;
        resp.message="成功";
        resp.data="";
        return resp;
    }
    public static <T> ApiResponse<T> ok(T data){
        ApiResponse resp = new ApiResponse();
        resp.code=SUCCESS_CODE;
        resp.message="成功";
        resp.data=data;
        return resp;
    }
    public static ApiResponse error(){
        ApiResponse resp = new ApiResponse();
        resp.code=ERROR_CODE;
        resp.message="失败";
        return  resp;
    }
    public static ApiResponse error(String message){
        ApiResponse resp = new ApiResponse();
        resp.code=ERROR_CODE;
        resp.message=message;
        return  resp;
    }
    public static ApiResponse error(int code,String message){
        ApiResponse resp = new ApiResponse();
        resp.code=code;
        resp.message=message;
        return  resp;
    }

    public ApiResponse message(String message){
        this.message=message;
        return this;
    }
    public ApiResponse code(int code){
        this.code=code;
        return this;
    }
    public T getData(){
        return data;
    }
}
